layui.define(['jquery','form', 'element', 'YsModal'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_TB_UserTaxManage';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			t.c.id_tab_1 = 'id_tab_1_'+s;
			t.c.id_tab_2 = 'id_tab_2_' + s;
			t.c.id_grid_1 = 'id_grid_1_' + s;
			t.c.id_grid_2 = 'id_grid_2_' + s;
			t.c.data = c.data; // 员工个税税率表
			t.c.data2 = c.data2; // 员工个税管理子表
			t.c.taxManageId = c.taxManageId;
			
			/**自定义的字段，是否流转中**/
			t.c.onProcess = c.onProcess;

			//复选框选中，存放行号，表1
			t.c.checkedIndex_1 = [];
			t.c.checkedIndex_2 = [];

		},
		load:function () { // 加载控件
			var h = ``
				+ `<div class="layui-tab layui-tab-brief" style="background-color: #ffffff;padding-top: 10px;padding-bottom: 10px;">`
					+ `<ul class="layui-tab-title">`
						+ `<li class="layui-this">1、员工个税税率表</li>`
						+ `<li>2、员工个税管理子表</li>`
					+ `</ul>`

					+ `<div class="layui-tab-content">`
						+ `<div class="layui-tab-item layui-show">`
							+ `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;" id="` + this.c.id_tab_1 + `">`
								+ `<div class="layui-table-tool-temp">`;
								if (this.c.onProcess == 0) {
									h+=`<div class="layui-btn-container" style="padding-bottom: 10px;padding-left: 10px;">`
										+ `<a class="layui-btn layui-btn-normal layui-btn-sm " lay-event="event-to-add-1"><i class="iconfont icon-add-btn"></i>新增</a>`
										+ `<a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="event-to-del-1" ><i class="layui-icon layui-icon-delete"></i>删除</a>`
									+ `</div>`;
								}
								h+= `<div class="layui-form layui-form-item" pane>`
										+ `<div id="` + this.c.id_grid_1 + `">`
											+ this.loadTable()
										+ `</div>`
									+ `</div>`
								+ `</div>`
							+ `</div>`
						+ `</div>`
						+ `<div class="layui-tab-item">`
							+ `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;" id="` + this.c.id_tab_2 + `">`
								+ `<div class="layui-table-tool-temp" style="padding-right: 0;">`;
								if (this.c.onProcess == 0) {
									h+= `<div class="layui-btn-container" style="padding-bottom: 10px;padding-left: 10px;">`
										+ `<a class="layui-btn layui-btn-normal layui-btn-sm " lay-event="event-to-add-2"><i class="iconfont icon-add-btn"></i>新增</a>`
										+ `<a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="event-to-del-2" ><i class="layui-icon layui-icon-delete"></i>删除</a>`
									+ `</div>`;
								}
									h+= `<div class="layui-form layui-form-item" pane>`
										+ `<div id="` + this.c.id_grid_2 + `">`
											+ this.loadGrid_2()
										+ `</div>`
									+ `</div>`
								+ `</div>`
							+ `</div>`
						+ `</div>`
					+ `</div>`
				+ `</div>`;
			$('#'+this.c.id).html(h);
			this.listenAll();
		},
		loadTable:function () { // 加载表格
			return ``
			+ `<div class="layui-table-box">`
				+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
					+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
						+ `<thead>`
							+ this.loadTableOfThead()
						+ `</thead>`
						+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
					+ `</table>`
				+ `</div>`
			+ `</div>`;
		},
		loadTableOfThead:function () { // 加载表头
			var h = `<thead>`;
			h +=`<tr>`;
			if(this.c.onProcess == 0) {//可编辑
				h += `<th style="width:1.9%; text-align: center; color: #000000;padding: 0 !important;">
							<input type="checkbox" id="checkAll" lay-skin="primary" lay-filter="checkAll">
						</th>`;
			}
			h += `<th style="width:5%; text-align: center; color: #000000;">档位</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">全年所得税区间</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">税率</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">速算扣除数</th>`;

			h + `</thead>`;
			return h;
		},
		loadTableOfTbody:function () { // 加载表tbody
			var t = this;
			var h = `<tbody id="tbody">`;
			for(var i=0;i<t.c.data.length;i++){
				h += `<tr id="table_th_`+i+`">`;
				h += t.loadTableTdOfTbody(t.c.data[i],i);
				h+= `</tr>`;
			}
			h += `</tbody>`;
			return h;
		},
		loadTableTdOfTbody:function (data,i) { // 加载表格行
			var h = ``;
			if(this.c.onProcess == 0) {//可编辑
				h += `<td style="text-align:center;padding:0;">
					<input type="checkbox" class="chBox"  lay-skin="primary" data-index="` + i + `"  lay-filter="checkBoxOne">
					</td>`;
			}
			if(this.c.onProcess == 0) {//可编辑
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;"><input type="number" maxlength="2"  class="text_align_center layui-input" data-index="` + i + `" data-type="gearNo" lay-filter="_input"  value="` + (data.gearNo ? data.gearNo : ``) + `" ></td>`;
				h += `<td style="text-align:center; width:20%;padding: 0 !important;border-width: 1px;">
					<input type="number" maxlength="10" class="text_align_center layui-input" style="width: 45%;float: left;" data-index="` + i + `" data-type="yearTaxMin" lay-filter="_input"  value="` + (data.yearTaxMin!=undefined ? data.yearTaxMin : ``) + `" >
					<div style="float: left;line-height:30px;padding-left: 10px;padding-right: 10px;"> ＜X≤ </div>
					<input type="number" maxlength="10" class="text_align_center layui-input" style="width: 45%;float: left;" data-index="` + i + `" data-type="yearTaxMax" lay-filter="_input"  value="` + (data.yearTaxMax!=undefined ? data.yearTaxMax : ``) + `" >
				</td>`;
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;">
						<input type="number" maxlength="20" class="text_align_center layui-input" style="width: 85%;float: left;" data-index="` + i + `" data-type="taxRate" lay-filter="_input"  value="` + (data.taxRate!=undefined ? data.taxRate : ``) + `" >
						<div style="float: left;line-height:30px;padding-left: 10px;padding-right: 10px;">%</div>
						</td>`;
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;"><input type="number" maxlength="20" class="text_align_center layui-input" data-index="` + i + `" data-type="deductNum" lay-filter="_input"  value="` + (data.deductNum!=undefined ? data.deductNum : ``) + `" ></td>`;
			}else {
				h += `<td style="text-align:center; padding: 0;">` + data.gearNo + `</td>`;
				h += `<td style="text-align:center; padding: 0;">` + (data.yearTaxMin!=undefined ? data.yearTaxMin : ``) + `＜X≤` + (data.yearTaxMax!=undefined ? data.yearTaxMax : ``) + `</td>`;
				h += `<td style="text-align:center; padding: 0;">` + data.taxRate+ `%` + `</td>`;
				h += `<td style="text-align:center; padding: 0;">` + data.deductNum + `</td>`;
			}
			return h;
		},
		loadGrid_2:function () { // 加载表格
			return `<table class="layui-table" lay-skin="row" style="margin:0;"><thead>`+this.loadGrid_2_OfThead()+`</thead><tbody>`+this.loadGrid_2_OfTbody()+`</tbody></table>`;
		},
		reloadGrid_2:function () { // 重新加载第二张表格
			$('#'+this.c.id_grid_2).html(this.loadGrid_2());
			this.listenGrid_2();
		},
		loadGrid_2_OfThead:function () { // 加载表头
			var h = `<thead><tr>`;
			if(this.c.onProcess == 0) {//可编辑
				h += `<th style="width:1.9%; text-align: center; color: #000000;padding: 0 !important;">
							<input type="checkbox" id="checkAll_2" lay-skin="primary" lay-filter="checkAll_2">
						</th>`;
			}
			h += `<th style="width:8%; text-align: center;color: #000000;padding: 0 !important;">工号/姓名</th>`;
			h += `<th style="width:8%; text-align: center;color: #000000;padding: 0 !important;">部门</th>`;
			h += `<th style="width:8%; text-align: center;color: #000000;padding: 0 !important;">员工状态</th>`;
			h += `<th style="width:8%; text-align: center;color: #000000;padding: 0 !important;">专项附加扣除</th>`;
			return h;
		},
		loadGrid_2_OfTbody:function () { // 加载表tbody
			var t = this;
			var h = `<tbody id="tbody_2">`;
			for(var i=0;i<t.c.data2.length;i++){
				h += `<tr id="table_2_th_`+i+`">`;
				h += t.loadTableTdOfTbody_2(t.c.data2[i],i);
				h+= `</tr>`;
			}
			h += `</tbody>`;
			return h;
		},
		loadTableTdOfTbody_2:function (data,i) { // 加载表格行
			var userStateTxt = '';
			if(data.userState!=undefined && data.userState!=null) {
				if(data.userState==0){
					userStateTxt = '实习';
				}else if(data.userState==1){
					userStateTxt = '试用';
				}else if(data.userState==2){
					userStateTxt = '正式';
				}else if(data.userState==3){
					userStateTxt = '离职';
				}
			}

			var h = ``;
			if(this.c.onProcess == 0) {//可编辑
				h += `<td style="text-align:center;padding:0;">
					<input type="checkbox" class="chBox_2"  lay-skin="primary" data-index="` + i + `"  lay-filter="checkBoxOne_2">
					</td>`;
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;"><input type="text" class="layui-input" value="` + (data._ys_nameLinked ? data._ys_nameLinked : '') + `" readonly="readonly"><i style="position: absolute; right: 2px; top:0px; height: 30px; line-height: 30px; width: 25px;" class="layui-icon layui-icon-more" data-type="_ys_nameLinked" data-index="` + i + `" lay-filter="buttontextModelByUserName"></i></td>`;
			}else {
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;">` + data._ys_nameLinked + `</td>`;
			}
			h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;">` + data.deptName + `</td>`;
			h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;">` + userStateTxt + `</td>`;
			if(this.c.onProcess == 0) {//可编辑
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;"><input type="number" maxlength="10" class="text_align_center layui-input" data-index="` + i + `" data-type="specialDeductNum" lay-filter="_input"  value="` + (data.specialDeductNum ? data.specialDeductNum : ``) + `" ></td>`;
			}else {
				h += `<td style="text-align:center; width:8%;padding: 0 !important;border-width: 1px;">` + data.specialDeductNum + `</td>`;
			}

			return h;
		},


		listenAll:function () { // 添加监听
			var t = this;
			// 监听标签页1
			t.listenTab_1();
			// 监听标签页2
			t.listenTab_2();
			// 表单控件重新加载
			form.render();
		},
		listenTab_1:function () { // 添加监听1
			var t = this;

			var domTab = $('#' + t.c.id_tab_1);

			// 监听“新增”按钮
			domTab.find('a[lay-event="event-to-add-1"]').on('click',function (e) {
				var rowCurrent = {};
				rowCurrent.id = null;
				rowCurrent.taxManageId = t.c.taxManageId;
				rowCurrent.taxLevyCriterion = '';
				rowCurrent.gearNo = '';
				rowCurrent.yearTaxMinAndMax = '';
				rowCurrent.yearTaxMin = '';
				rowCurrent.yearTaxMax = '';
				rowCurrent.taxRate = '';
				rowCurrent.deductNum = '';
				t.addRow(rowCurrent);
			});

			// 监听“删除”按钮
			$('a[lay-event="event-to-del-1"]').on('click',function (e) {
				var checkedIndexList = t.c.checkedIndex_1;
				if (checkedIndexList.length ==0 || checkedIndexList==null){
					return layer.msg("请至少选择一条要删除的数据!");
				}
				var oldData = [];
				for (var i = 0; i < t.c.data.length; i++) {
					for (var j = 0; j < checkedIndexList.length; j++) {
						if(i == checkedIndexList[j]){
							oldData.push(t.c.data[i].id);
						}
					}
				}
				if(oldData.length>0){
					t.delYes(oldData,checkedIndexList,1);
				}
			}),

			//全选
			layui.form.on('checkbox(checkAll)', function(data){
				var elem = data.elem;
				$(".chBox").prop("checked", elem.checked);
				var checkedIndex = [];
				$(".chBox").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex_1 = checkedIndex;
				layui.form.render();
			});
			// 列表里的小选择框点击
			layui.form.on('checkbox(checkBoxOne)', function(data){
				var elem = data.elem;
				//console.log('elem',elem);
				if ($(".chBox").not('input:checked').length) {
					$('#checkAll').prop("checked", false);
				} else {
					$('#checkAll').prop("checked", true);
				}
				var checkedIndex = [];
				$(".chBox").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex_1 = checkedIndex;
				layui.form.render();
			});

			t.listenGrid_1();
		},
		listenTab_2:function () { // 添加监听2
			var t = this;
			var domTab = $('#' + t.c.id_tab_2);

			// 监听“新增”按钮
			domTab.find('a[lay-event="event-to-add-2"]').on('click',function (e) {
				var rowCurrent = {};
				rowCurrent.id = null;
				rowCurrent.taxManageId = t.c.taxManageId;
				rowCurrent.userId = '';
				rowCurrent._ys_nameLinked = '';
				rowCurrent.specialDeductNum = '';
				rowCurrent.deptName = '';
				rowCurrent.userState = '';
				t.addRow2(rowCurrent);
			});

			// 监听“删除”按钮
			$('a[lay-event="event-to-del-2"]').on('click',function (e) {
				var checkedIndexList = t.c.checkedIndex_2;
				if (checkedIndexList.length ==0 || checkedIndexList==null){
					return layer.msg("请至少选择一条要删除的数据!");
				}
				var oldData = [];
				for (var i = 0; i < t.c.data2.length; i++) {
					for (var j = 0; j < checkedIndexList.length; j++) {
						if(i == checkedIndexList[j]){
							oldData.push(t.c.data2[i].id);
						}
					}
				}
				if(oldData.length>0){
					t.delYes(oldData,checkedIndexList,2);
				}
			}),


			//全选
			layui.form.on('checkbox(checkAll_2)', function(data){
				var elem = data.elem;
				$(".chBox_2").prop("checked", elem.checked);
				var checkedIndex = [];
				$(".chBox_2").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex_2 = checkedIndex;
				layui.form.render();
			});
			
			// 列表里的小选择框点击
			layui.form.on('checkbox(checkBoxOne_2)', function(data){
				var elem = data.elem;
				if ($(".chBox_2").not('input:checked').length) {
					$('#checkAll_2').prop("checked", false);
				} else {
					$('#checkAll_2').prop("checked", true);
				}
				var checkedIndex = [];
				$(".chBox_2").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex_2 = checkedIndex;
				layui.form.render();
			});
			t.listenGrid_2();
		},

		listenGrid_1:function () { // 添加表格监听1
			var t = this;
			var domGrid = $('#' + t.c.id_grid_1);

			//先移除监听、再添加监听
			//$("input").off("click");
			domGrid.find('input[lay-filter="_input"]').off("click");
			// 监听各个输入框
			domGrid.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});

			// 表单控件重新加载
			form.render();
		},
		listenGrid_2:function () { // 添加表格监听2
			var t = this;
			var domGrid = $('#' + t.c.id_grid_2);

			//先移除监听、再添加监听
			//$("input").off("click");
			domGrid.find('input[lay-filter="_input"]').off("click");
			// 监听各个输入框
			domGrid.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data2[index][key] = value;
				return;
			});

			//先移除监听、再添加监听
			//$("i").off("click");
			domGrid.find('i[lay-filter="buttontextModelByUserName"]').off("click");
			// 监听弹出框
			domGrid.find('i[lay-filter="buttontextModelByUserName"]').on('click',function (e) {
				var indexOfRow = $(this).attr('data-index');
				layui.YsModal.init({modalKey: 'TS_UserInfo', colCode: indexOfRow, parentObj: t, extraData: {useIn: 'chooseUserName'}});
			});

			// 表单控件重新加载
			form.render();
		},
		//确定删除
		delYes:function(spId,checkedIndexList,delType){
			var t = this;
			var c = this.c;
			var loadIndex = layer.load(2);
			var urlStr = '';
			if(delType==1){
				urlStr = 'toDelTaxRate';
			}else{
				urlStr = 'toDelItem';
			}
			$.ajax({
				url: '/api/inter/tbUserTaxManage/'+urlStr,
				data: "list="+spId,
				type: 'POST',
				async: true,
				success: function (result) {
					if(result.code==0){
						layer.close(loadIndex);
						layer.msg("操作成功", {icon: 1});
						var newData = [];
						var oldData = [];
						if(delType==1){
							oldData = c.data;
						}else{
							oldData = c.data2;
						}
						for (var i = 0; i < oldData.length; i++) {
							var isDel = false;
							for (var j = 0; j < spId.length; j++) {
								if(oldData[i].id==spId[j]){
									isDel = true;
								}
							}
							if(!isDel){
								newData.push(oldData[i]);
							}
						}
						if(delType==1){
							c.data = newData;
						}else{
							c.data2 = newData;
						}
						$('#' + c.id).empty();
						return t.reload(c);
					}else {//失败
						layer.close(loadIndex);
						layer.msg(result.msg, {icon: 2});
					}
				},
				error: function () {
					layer.close(loadIndex);
					layer.msg("删除失败，请稍后再试！", {icon: 2});
				}
			});
		},

		removeRow:function(index) { // 重新加载表格
			var t = this;
			t.c.data.splice(index, 1);
			return t.reload(t.c);
		},
		getData:function () {
			return {'taxRateList':this.c.data, 'itemList':this.c.data2};
		},
		init:function (c) {this.f(c);this.load();return object;},
		reload:function (c) {
			c.data = this.c.data;
			c.data2 = this.c.data2;
			$('#' + this.c.id).empty();
			return this.init(c);
		},
		addRow:function (row) {
			var c = this.c;
			var t = this;
			c.data.push(row);
			var h =  `<tr id="table_th_`+(c.data.length-1)+`">`;
			h += t.loadTableTdOfTbody(row,c.data.length-1);
			h+= `</tr>`;
			$('#tbody').append(h);
			t.listenGrid_1();
		},
        addRow2:function (row) {
			var c = this.c;
			var t = this;
			c.data2.push(row);
			var h = `<tr id="table_2_th_`+(c.data2.length-1)+`">`
			+ t.loadTableTdOfTbody_2(row,c.data2.length-1)
			+ `</tr>`;
			$('#tbody_2').append(h);
			t.listenGrid_2();
        },
		addRows:function (rows) {
			var c = this.c;
			for (var i = 0; i < rows.length; i++) {
				c.data.push(rows[i]);
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRow:function (index, row) {
			var c = this.c;
			c.data[index] = row;
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRowOfAttr:function (index, row) {
			var c = this.c;
			for(var key in row){
				c.data[index][key] = row[key]
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		reloadRows:function (rows) {
			var c = this.c;
			c.data = rows;
			return this.reload(c);
		},
		//单选弹出框返回
		matchValuesAfterModalSelectRadio:function(colCode, data, extraData) {
			console.log('选择的',data);
			console.log('extraData',extraData);
			var t = this;
			if(extraData.useIn == 'chooseUserName') {
				t.c.data2[colCode].userId = data.userId;
				t.c.data2[colCode]._ys_nameLinked = data.userId +'/'+data.userName;
				t.c.data2[colCode].deptName = data.deptName;
				t.c.data2[colCode].userState = data.userState;
				var h = t.loadTableTdOfTbody_2(t.c.data2[Number(colCode)],Number(colCode));
				$('#table_2_th_' + colCode).html(h);
				t.listenGrid_2();
        	}
		},
	};
	exports('simple_table_TB_UserTaxManage', object);
});